#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct node {
	int w, v;
	node(int wi, int vi) {
		w = wi;
		v = vi;
	}
};

struct point {
	int w, id;
	point(int wi, int i) {
		w = wi;
		id = i;
	}
	bool operator < (const point x)const {
		return x.w < this->w;
	}
};

int n;
vector<node> G[1005];
priority_queue<point> h;
bool S[1005];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	while (1) {
		cin >> n;
		if (n == 0) break;
		int si, wi, pi, u;
		char tmp;
		for (int i = 1; i < n; ++i) {
			cin >> tmp >> pi;
			si = tmp + 1 - 'A';
			//printf("+1:%c %d %d\n", tmp, si, pi);
			for (int j = 1; j <= pi; ++j) {
				cin >> tmp >> wi;
				u = tmp + 1 - 'A';
				//printf("+2:%c %d %d\n", tmp, u, wi);
				cout << "+" << u << " " << si << " " << wi << endl;
				G[si].push_back(node(u, wi));
				G[u].push_back(node(si, wi));
			}
		}
		
		int ans = 0;
		int index = 1;
		h.push(point(0, 1));
		while (index <= n) {
			int x = h.top().id, d = h.top().w;
			h.pop();
			if (S[x]) continue;
			S[x] = 1;
			++index;
			ans += d;
			for (unsigned i = 0; i < G[x].size(); ++i) {
				int y = G[x][i].v, z = G[x][i].w;
				if (!S[y]) {
					h.push(point(z, y));
				}
			}
		}
		cout << ans << "\n";
		for (int i = 0; i <= n + 1; ++i) {
			G[i].clear();
		}
		while (h.size()) {
			h.pop();
		}
		for (int i = 0; i <= n; ++i) {
			S[i] = 0;
		}
	}
	return 0;
}
